﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebMvc.App.Web.Default.postgresqlTrans
{
    public class queryRowAction  : Controller
	{
		public queryRowAction(System.IO.TextWriter tw):base(tw){}
		public queryRowAction(string fileName) : base(fileName) {}
		public queryRowAction(HttpContext context):base(context){}
		public queryRowAction(Microsoft.Owin.IOwinContext context):base(context){}
        
        
        
        
        
        
        
        public class __queryRow_user__:NFinal.Struct
		{
			public System.String name;
			public System.String pwd;
			public System.Int32? id;
			#region 写Json字符串
			public override void WriteJson(System.IO.TextWriter tw)
			{
				tw.Write("{");
						tw.Write("\"name\":");
						if(name==null)
						{
							tw.Write("null");
						}
						else
						{
							tw.Write("\"");
							tw.Write(name);
							tw.Write("\"");
						}
						tw.Write(",");
						tw.Write("\"pwd\":");
						if(pwd==null)
						{
							tw.Write("null");
						}
						else
						{
							tw.Write("\"");
							tw.Write(pwd);
							tw.Write("\"");
						}
						tw.Write(",");
						tw.Write("\"id\":");
						tw.Write(id==null?"null" : id.ToString());
				tw.Write("}");
			}
			#endregion
		}
		public void queryRow(int id)
        {
            var con = new Npgsql.NpgsqlConnection(NFinal.Config.ConfigurationManager.ConnectionStrings["PostgreSql"].ConnectionString);
			con.Open();
            var trans = con.BeginTransaction();
            try
            {
                #region	var user; 选取一行
			var user = new __queryRow_user__();
			var __queryRow_user_command__ = new Npgsql.NpgsqlCommand("select * from users where id=@id", con);
			__queryRow_user_command__.Transaction=trans;
			var __queryRow_user_parameters__=new Npgsql.NpgsqlParameter[1];
			__queryRow_user_parameters__[0] = new Npgsql.NpgsqlParameter("@id",NpgsqlTypes.NpgsqlDbType.Integer,4);
			__queryRow_user_parameters__[0].Value = id;
			__queryRow_user_command__.Parameters.AddRange(__queryRow_user_parameters__);
			var __queryRow_user_reader__= __queryRow_user_command__.ExecuteReader();
			if (__queryRow_user_reader__.HasRows)
			{
				__queryRow_user_reader__.Read();
				user = new __queryRow_user__();
					user.name =__queryRow_user_reader__.IsDBNull(0)?null: __queryRow_user_reader__.GetString(0);
					user.pwd =__queryRow_user_reader__.IsDBNull(1)?null: __queryRow_user_reader__.GetString(1);
					if(!__queryRow_user_reader__.IsDBNull(2)){user.id = __queryRow_user_reader__.GetInt32(2);}
			}
			else
			{
				user = null;
			}
			__queryRow_user_reader__.Dispose();
			__queryRow_user_command__.Dispose();
			#endregion
			
                trans.Commit();
                AjaxReturn(user);
            }
            catch
            {
                trans.Rollback();
            }
            con.Close();
        }
        
    }
}